@echo off
REM GitDiagram 完整启动脚本 - Windows版 (适用于无代码情况)

echo ===== GitDiagram 一键部署工具 =====
echo 此脚本将从GitHub下载GitDiagram并启动完整环境

REM 检查Docker是否已安装
where docker >nul 2>nul
if %ERRORLEVEL% neq 0 (
  echo 错误：Docker未安装。请先安装Docker。
  echo 安装指南: https://docs.docker.com/desktop/install/windows-install/
  exit /b 1
)

REM 检查Git是否已安装
where git >nul 2>nul
if %ERRORLEVEL% neq 0 (
  echo 错误：Git未安装。请先安装Git。
  echo 安装指南: https://git-scm.com/downloads
  exit /b 1
)

REM 检查docker compose命令
docker compose version >nul 2>nul
if %ERRORLEVEL% neq 0 (
  echo 警告：未检测到docker compose命令，请确保Docker Desktop已安装并更新至最新版本
  exit /b 1
)

REM 创建临时工作目录
set WORK_DIR=gitdiagram-docker
if exist %WORK_DIR%\ (
  echo 目录 %WORK_DIR% 已存在。是否删除现有目录并重新下载? [y/N]
  set /p response=
  if /i "%response%"=="y" (
    rmdir /s /q %WORK_DIR%
    goto clone_repo
  ) else (
    echo 将使用现有目录
    cd %WORK_DIR%
    if exist docker-compose.dev.yml (
      echo 找到docker-compose.dev.yml，跳过下载步骤
      goto setup_env
    )
  )
) else (
  :clone_repo
  echo 克隆GitDiagram代码...
  git clone https://github.com/ahmedkhaleel2004/gitdiagram.git %WORK_DIR%
  cd %WORK_DIR%
  
  echo 创建Docker部署文件...
  
  REM 创建docker-compose.dev.yml
  echo version: '3.8'> docker-compose.dev.yml
  echo.>> docker-compose.dev.yml
  echo services:>> docker-compose.dev.yml
  echo   # 前端服务>> docker-compose.dev.yml
  echo   frontend:>> docker-compose.dev.yml
  echo     build:>> docker-compose.dev.yml
  echo       context: .>> docker-compose.dev.yml
  echo       dockerfile: Dockerfile.frontend>> docker-compose.dev.yml
  echo     ports:>> docker-compose.dev.yml
  echo       - "3000:3000">> docker-compose.dev.yml
  echo     volumes:>> docker-compose.dev.yml
  echo       - ./src:/app/src>> docker-compose.dev.yml
  echo       - ./public:/app/public>> docker-compose.dev.yml
  echo     environment:>> docker-compose.dev.yml
  echo       - NEXT_PUBLIC_API_DEV_URL=http://api:8000>> docker-compose.dev.yml
  echo     depends_on:>> docker-compose.dev.yml
  echo       - api>> docker-compose.dev.yml
  echo     restart: unless-stopped>> docker-compose.dev.yml
  echo.>> docker-compose.dev.yml
  echo   # 后端API服务>> docker-compose.dev.yml
  echo   api:>> docker-compose.dev.yml
  echo     build: >> docker-compose.dev.yml
  echo       context: ./backend>> docker-compose.dev.yml
  echo       dockerfile: Dockerfile>> docker-compose.dev.yml
  echo     ports:>> docker-compose.dev.yml
  echo       - "8000:8000">> docker-compose.dev.yml
  echo     volumes:>> docker-compose.dev.yml
  echo       - ./backend:/app>> docker-compose.dev.yml
  echo     env_file:>> docker-compose.dev.yml
  echo       - .env>> docker-compose.dev.yml
  echo     environment:>> docker-compose.dev.yml
  echo       - ENVIRONMENT=${ENVIRONMENT:-development}>> docker-compose.dev.yml
  echo       - POSTGRES_URL=postgresql://postgres:${DB_PASSWORD:-postgres}@db:5432/gitdiagram>> docker-compose.dev.yml
  echo     depends_on:>> docker-compose.dev.yml
  echo       - db>> docker-compose.dev.yml
  echo     dns:>> docker-compose.dev.yml
  echo       - 8.8.8.8>> docker-compose.dev.yml
  echo       - 8.8.4.4>> docker-compose.dev.yml
  echo     restart: unless-stopped>> docker-compose.dev.yml
  echo.>> docker-compose.dev.yml
  echo   # 数据库服务>> docker-compose.dev.yml
  echo   db:>> docker-compose.dev.yml
  echo     image: postgres:15-alpine>> docker-compose.dev.yml
  echo     ports:>> docker-compose.dev.yml
  echo       - "5432:5432">> docker-compose.dev.yml
  echo     volumes:>> docker-compose.dev.yml
  echo       - gitdiagram_data:/var/lib/postgresql/data>> docker-compose.dev.yml
  echo     environment:>> docker-compose.dev.yml
  echo       - POSTGRES_PASSWORD=${DB_PASSWORD:-postgres}>> docker-compose.dev.yml
  echo       - POSTGRES_USER=postgres>> docker-compose.dev.yml
  echo       - POSTGRES_DB=gitdiagram>> docker-compose.dev.yml
  echo     restart: unless-stopped>> docker-compose.dev.yml
  echo     healthcheck:>> docker-compose.dev.yml
  echo       test: ["CMD-SHELL", "pg_isready -U postgres"]>> docker-compose.dev.yml
  echo       interval: 5s>> docker-compose.dev.yml
  echo       timeout: 5s>> docker-compose.dev.yml
  echo       retries: 5>> docker-compose.dev.yml
  echo.>> docker-compose.dev.yml
  echo volumes:>> docker-compose.dev.yml
  echo   gitdiagram_data:>> docker-compose.dev.yml
  echo     driver: local>> docker-compose.dev.yml

  REM 创建Dockerfile.frontend
  echo FROM node:20-alpine> Dockerfile.frontend
  echo.>> Dockerfile.frontend
  echo # 设置工作目录>> Dockerfile.frontend
  echo WORKDIR /app>> Dockerfile.frontend
  echo.>> Dockerfile.frontend
  echo # 安装pnpm>> Dockerfile.frontend
  echo RUN npm install -g pnpm>> Dockerfile.frontend
  echo.>> Dockerfile.frontend
  echo # 复制package.json和pnpm-lock.yaml>> Dockerfile.frontend
  echo COPY package.json pnpm-lock.yaml ./>> Dockerfile.frontend
  echo.>> Dockerfile.frontend
  echo # 安装依赖>> Dockerfile.frontend
  echo RUN pnpm install>> Dockerfile.frontend
  echo.>> Dockerfile.frontend
  echo # 复制其余文件>> Dockerfile.frontend
  echo COPY . .>> Dockerfile.frontend
  echo.>> Dockerfile.frontend
  echo # 构建应用>> Dockerfile.frontend
  echo RUN pnpm build>> Dockerfile.frontend
  echo.>> Dockerfile.frontend
  echo # 暴露端口>> Dockerfile.frontend
  echo EXPOSE 3000>> Dockerfile.frontend
  echo.>> Dockerfile.frontend
  echo # 启动命令>> Dockerfile.frontend
  echo CMD ["pnpm", "start"]>> Dockerfile.frontend
)

:setup_env
REM 检查.env文件
if not exist .env (
  echo 未找到.env文件
  if exist .env.example (
    echo 从.env.example创建.env文件
    copy .env.example .env
  ) else (
    echo 创建基本的.env文件
    echo # 数据库配置> .env
    echo POSTGRES_URL=postgresql://postgres:postgres@db:5432/gitdiagram>> .env
    echo DB_PASSWORD=postgres>> .env
    echo.>> .env
    echo # OpenAI配置>> .env
    echo OPENAI_API_KEY=>> .env
    echo.>> .env
    echo # 启用HTTPS和API密钥检查>> .env
    echo ENABLE_HTTPS=false>> .env
    echo ENABLE_API_KEY=false>> .env
    echo.>> .env
    echo # 环境设置>> .env
    echo ENVIRONMENT=development>> .env
  )
  echo .env文件已创建
  echo 请编辑.env文件添加您的OpenAI API密钥
  echo 是否立即编辑.env文件? [y/N]
  set /p edit_response=
  if /i "%edit_response%"=="y" (
    notepad .env
  )
)

REM 构建并启动容器
echo 启动GitDiagram服务...
docker compose -f docker-compose.dev.yml up -d

REM 等待服务启动
echo 检查服务状态...
timeout /t 5 /nobreak >nul

REM 检查服务状态
docker compose -f docker-compose.dev.yml ps
echo.
echo GitDiagram服务已启动!
echo 前端地址: http://localhost:3000
echo 后端API地址: http://localhost:8000
echo 首次启动可能需要一些时间加载，请耐心等待
echo.
echo 后续使用说明:
echo 1. 停止服务: docker compose -f docker-compose.dev.yml down
echo 2. 重启服务: docker compose -f docker-compose.dev.yml up -d
echo 3. 查看日志: docker compose -f docker-compose.dev.yml logs -f
echo 4. 项目目录: %CD%

pause 